package com.gomeplus.security.md5;

import java.security.MessageDigest;

/**
 * @ClassName: MD5Util
 * @Description: MD5签名
 * @author chenmin-ds
 * @date 2017年5月26日 下午1:49:41
 */
public class MD5Util {

	/**
	 * @Title: encodeMessage
	 * @Description:  MD5签名方法
	 * @param data
	 * @throws Exception
	 * @return String 返回类型
	 */
	public static String encodeMessage(byte[] data) throws Exception {
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		md5.update(data);
		return toHex(md5.digest());
	}

	private static String toHex(byte[] buffer) {
		byte[] result = new byte[buffer.length * 2];
		byte[] temp;
		for (int i = 0; i < buffer.length; i++) {
			temp = getHexValue(buffer[i]);
			result[i * 2] = temp[0];
			result[i * 2 + 1] = temp[1];
		}
		return new String(result).toUpperCase();
	}

	private static byte[] getHexValue(byte b) {
		int value = b;
		if (value < 0) {
			value = (int) (256 + b);
		}
		String s = Integer.toHexString(value);
		if (s.length() == 1) {
			return new byte[] { '0', (byte) s.charAt(0) };
		}
		return new byte[] { (byte) s.charAt(0), (byte) s.charAt(1) };
	}
}
